package com.projectfile.controller;



import com.projectfile.entity.Money;
import com.projectfile.entity.User;
import com.projectfile.model.JsonResult;
import com.projectfile.service.UserSrvImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class UserCtrl {
    @Resource
    private UserSrvImpl srv;

    @Resource
    private RedisTemplate redisTemplate;
    @CrossOrigin
    @GetMapping("/save/{userid}/{user_money}")//存钱
    public JsonResult save(User user){
        JsonResult res=new JsonResult(0,"存钱成功");
        try {
            Date date = new Date(System.currentTimeMillis());
            User users=new User(user.getUserid(),user.getUser_money());
            Money money=new Money("存钱",user.getUser_money(),date,user.getUserid());
            redisTemplate.opsForList().leftPush("his_"+user.getUserid(),money);
            srv.save(users);
            redisTemplate.boundValueOps("money_"+user.getUserid()).increment(user.getUser_money());
            srv.addsave(money);
            res.setData(users);
        }catch (Exception e){
            e.printStackTrace();
            res.setCode(1);
            res.setMsg("存钱失败");

        }
        return res;
    }
    @CrossOrigin
    @GetMapping("/withdraw/{userid}/{user_money}")//取钱
    public JsonResult withdraw(User user) {
        JsonResult res = new JsonResult(0, "取钱成功");
//      LoginUser user = new LoginUser(userid, Integer.parseInt(money_amount));
        User user1=srv.balance(user.getUserid());
        try {
            if(user.getUser_money()>user1.getUser_money()){
                res.setCode(2);
                res.setMsg("您的余额不足，请查询余额");
            }else {

//                User user = new User(userid, money_amount);
                Date date = new Date(System.currentTimeMillis());
//                String uuid = UUID.randomUUID().toString().replace("-", "");
                Money money = new Money("取钱", user.getUser_money(), date, user.getUserid());
                redisTemplate.opsForList().leftPush("his_"+user.getUserid(),money);
                redisTemplate.boundValueOps("money_"+user.getUserid()).increment(-user.getUser_money());
                srv.withdraw(user);
                srv.addsave(money);
                res.setData(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(1);
        }
        return res;
    }
    @CrossOrigin
    @GetMapping("/balance/{userid}")//余额
    public JsonResult balance(User user){
        JsonResult res = new JsonResult(0,"余额为");
        String rediskey="money_"+user.getUserid();
        System.out.println(rediskey);
        int user1=(Integer) redisTemplate.boundValueOps("money_"+user.getUserid()).get();

//            try {
//               // LoginUser user=srv.balance(userid);
//                if(user!=null){
//                    String mysqlkey="money_"+user.getUserid();
//                    redisUtil.set(mysqlkey,user);
//                }else {
//                    String mysqlkey = "moneys_"+userid;
//                    redisUtil.set(mysqlkey,"none");
//                }
//                res.setData(user);
//            }catch (Exception e){
//                e.printStackTrace();
//                res.setCode(1);
//                res.setMsg("查询失败");
//            }
            res.setData(user1);
        System.out.println(user.getUserid());
        System.out.println(user.getUser_money());
        return res;
    }
    @CrossOrigin
    @GetMapping("/history/{userid}")//查询操作历史记录
    public JsonResult history(@PathVariable String userid){
        JsonResult res =new JsonResult(0,"查询成功");
        try {
            List<Money> list =redisTemplate.opsForList().range("his_"+userid,0,-1);
//            redisTemplate.opsForList().leftPush(userid,list);
//            List<Money> moneyList=redisTemplate.opsForList().range(userid,0,-1);
            List datas = new ArrayList();
            SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for(Money money:list){
                Map map = BeanUtils.describe(money);
                Date date =money.getMoney_createtime();
                //String createTime=(String)formatter.format(date);
                map.put("time", date);
                map.remove("class");
                datas.add(map);
            }
            res.setData(datas);
        }catch (Exception e){
            e.printStackTrace();
            res.setCode(1);
            res.setMsg("查询失败");
        }
        return res;
    }
}
